package br.com.rglariguet.pioneer.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import br.com.rglariguet.pioneer.dao.PecaDAO;
import br.com.rglariguet.pioneer.enums.StatusReparoAdminEnum;
import br.com.rglariguet.pioneer.modelos.Peca;
import br.com.rglariguet.pioneer.modelos.Usuario;

@Controller
@SessionAttributes("usuarioLogado")
public class LoginController{
	
	@Autowired
	private PecaDAO pecaDao;
	
	@RequestMapping("/mudaLingua")
	public String index(String lingua, HttpSession session){
	    Locale locale = new Locale(lingua);
	    Config.set(session, Config.FMT_LOCALE, locale);
	    Config.set(session, Config.FMT_FALLBACK_LOCALE, locale);
	    return "/usuarios/login-form";
	}
	
	@RequestMapping("/seguro/inicio")
	public String index(ModelMap model){
		Usuario user = (Usuario) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("usuarioLogado", user.getNome());
		
		List<StatusReparoAdminEnum> stateList = new ArrayList<StatusReparoAdminEnum>( Arrays.asList(StatusReparoAdminEnum.values() ));
		model.addAttribute("statusList", stateList);
		populaListaPecasAutoComplete();
		return "/seguro/index";
	}
	
	
	public static List<Peca> listaDePecas = new ArrayList<Peca>();
	
	private void populaListaPecasAutoComplete() {
		if (listaDePecas.isEmpty()) {
			for (Peca peca : pecaDao.listarTodos()) {
				listaDePecas.add(peca);
			}
		}
	}

	
	@RequestMapping(value = "/seguro/getPecas", method = RequestMethod.GET)
	public @ResponseBody List<Peca> getPecas(@RequestParam String nome) {
		return simulaBuscaAutocomplete(nome);
	}

	private List<Peca> simulaBuscaAutocomplete(String nome) {
		List<Peca> result = new ArrayList<Peca>();
		for (Peca peca : listaDePecas) {
			if (peca.getCodigo().toLowerCase().contains(nome.toLowerCase()) || peca.getDescricao().toLowerCase().contains(nome.toLowerCase())) {
				result.add(peca);
			}
		}
		return result;
	}
	
     
	@RequestMapping("/login-form")
	public String login(HttpSession session){
		Locale locale = new Locale("pt", "BR");
		Config.set(session, Config.FMT_LOCALE, locale);
	    Config.set(session, Config.FMT_FALLBACK_LOCALE, locale);
		return "/usuarios/login-form";
	}
	
	@RequestMapping("/loginerror")
	public String loginError(ModelMap model){
		model.addAttribute("erroLogin","Erro ao logar");
		return "/usuarios/login-form";
	}
	
}
